package com.zyf.service.web;


import com.zyf.service.config.StorageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
public class StorageController {

    @Autowired
    private StorageProperties storageProperties;


    //    @PostMapping("/uploadPreview")
//    public String uploadPreviewFile(HttpServletRequest request, @RequestPart("file") MultipartFile file) {
//        if(null == file) throw new BusException("上传空文件");
//        String storePath = storageService.store(file);
//        upLoadDto.setRelativePath("/preview/"+storePath);
//        //upLoadDto.setUrlPath(serverConfig.getUrl()+"/download/"+storePath);
//        upLoadDto.setUrlPath("http://"+request.getServerName()+":"+request.getServerPort()+"/preview/"+storePath);
//        //PersonInfoVo personInfoVo = new PersonInfoVo();
//        //User user= SecurityUtils.getLoginUser().getUser();
//        //personInfoVo.setUserId(user.getUserId());
//        //personInfoVo.setAvatar("http://"+request.getServerName()+":"+request.getServerPort()+"/preview/"+storePath);
//        //userService.UpdatePersonInfo(personInfoVo);
//        return new R().Success(upLoadDto);
//    }
    @GetMapping("/api/videos")
    public List videos(HttpServletResponse response) throws IOException {
        List json = storageProperties.videos();
        return json;
    }

    @GetMapping("1/preview/{dirName:.+}/{fileName:.+}")
    public void previewFile(@PathVariable String dirName,
                            @PathVariable String fileName, HttpServletResponse response) throws IOException {
        showImg("./" + storageProperties.getLocation() + "/" + dirName + "/" + fileName, response);
    }

    @GetMapping("1/preview/{fileName}")
    public void previewFile1(
            @PathVariable String fileName, HttpServletResponse response) throws IOException {
        showImg(storageProperties.getLocation() + "/" + fileName, response);
    }

    public static void showImg(String path, HttpServletResponse response) {
        if (path != null && !path.equals("")) {

            try {
                FileInputStream fis = new FileInputStream(path);
                ServletOutputStream os = response.getOutputStream();

                byte[] b = new byte[1024 * 8];
                while (fis.read(b) != -1) {
                    os.write(b);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private static void blockFfmpeg(BufferedReader br) throws IOException {
        String line;
        // 该方法阻塞线程，直至合成成功
        while ((line = br.readLine()) != null) {
            doNothing(line);
        }
    }

    private static void doNothing(String line) {
        System.out.println(line);
    }
}
